import React from 'react';
import { withRouter } from 'react-router';
import config from '../../assets/js/conf/config';
import './index.css';

class SubHeader extends React.Component {
    constructor(props){
        super(props);
        this.state = {}
    }

    //页面返回
    goBack(){
        const { pathname } = this.props.location;
        if(pathname === config.path+'address/index'){
            this.props.history.replace(config.path+'balance/index');
        }else{
            this.props.history.goBack();
        }
    }

    //右按钮点击事件
    getClick(){
        this.props.onClick && this.props.onClick();
    }

    componentWillUnmount(){
        //页面卸载时，重写setState,避免内存溢出
        this.setState = (state, callback) => {
            return ;
        }
    }
    
    render() {
        const { title, rightText } = this.props;
        return (
            <div className='sub-header'>
                <div className='back' onClick={() => this.goBack()}></div>
                <div className='title'>{title}</div>
                <div className={!!rightText ? 'right-btn' : 'right-btn hide'} onClick={() => this.getClick()}>{rightText}</div>
            </div>
        )
    }
}

export default withRouter(SubHeader);